namespace WiseLink.RDS.Web
{
    using System;
    using System.Web.Mvc;
    using WiseLink.RDS.Model;
    using Telerik.Web.Mvc;
    using System.Collections.Generic;
    using System.Linq;

    public partial class CustomerController : Controller
    {
        private CustomerRepository customerRepository;
        [GridAction]
        public ActionResult FirstLook()
        {

            return View(new GridModel<CustomerViewModel>
            {
                Data = customerRepository.GetAllCustomers()
            });

        }

        //SimpleCustomer for the customers dropdown
        [HttpPost]
        public ActionResult GetCustomerName()
        {

            return new JsonResult
            {
                Data = customerRepository.GetCustomerName()
            };

        }

        public ActionResult GetCustomerDetail(int customerID)
        {
           CustomerViewModel customer =  customerRepository.GetAllCustomers().Where(c=>c.CustomerID == customerID).FirstOrDefault();

           return PartialView("CustomerDetail", customer);

        }



        [GridAction]
        public ActionResult _InsertCustomerEditing(DateTime? selectedDate, DateTime? minDate, DateTime? maxDate)
        {
            CustomerViewModel customerVM = new CustomerViewModel();

            //ModelState.AddModelError("", new Exception("bad name"));

            
            //((ModelState)ModelState.Values.f[0]).Errors[0].Exception = new Exception("excetion bad name");
            //Perform model binding (fill the product properties and validate it).
            
            if (TryUpdateModel(customerVM))
            {
                //The model is valid - insert the product.
                customerRepository.Insert(customerVM);
            }


            return View(new GridModel<CustomerViewModel>
            {
                Data = customerRepository.GetAllCustomers()
            });

        }

        [GridAction]
        public ActionResult _UpdateCustomerEditing(int id)
        {

            CustomerViewModel customer = customerRepository.GetOneCustomer(id);
            if (TryUpdateModel(customer))
            {
                 customerRepository.Update(customer);
            }            
            //ModelState.AddModelError("", "bad name");
            
            return View(new GridModel<CustomerViewModel>
            {
                Data = customerRepository.GetAllCustomers()
            });
            

        }
        [GridAction]
        public ActionResult _DeleteCustomerEditing(int id)
        {

            customerRepository.Delete(id);

            return View(new GridModel<CustomerViewModel>
            {
                Data = customerRepository.GetAllCustomers()
            });

        }

        public ActionResult SecondLook()
        {

            return View();
        }
        public CustomerController()
        {
            customerRepository = new CustomerRepository();
        }
    }
}